package com.fantasy.servlet;

import com.fantasy.entity.Page;
import com.fantasy.entity.Video;
import com.fantasy.service.CommentService;
import com.fantasy.service.VideoService;
import com.fantasy.service.impl.CommentServiceImpl;
import com.fantasy.service.impl.VideoServiceImpl;
import com.fantasy.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;



@WebServlet("/admin/video.do")
public class VideoServlet extends BaseServlet {

    private VideoService videoService=new VideoServiceImpl();

    /**
     * 添加视频
     * @param request
     * @param response
     * @throws IOException
     * @throws ParseException
     */
    public void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        String videoId = request.getParameter("videoId");
        Integer videoUser= Integer.valueOf(request.getParameter("videoUser"));
        Integer videoType = Integer.valueOf(request.getParameter("videoType"));
        String videoDescribe = request.getParameter("videoDescribe");
        Integer videoExamine = Integer.valueOf(request.getParameter("videoExamine"));
        Integer videoIsviewable = Integer.valueOf(request.getParameter("videoIsviewable"));
        String videoUploadDate= request.getParameter("videoUploadDate");
        Integer videoStarNum = Integer.valueOf(request.getParameter("videoStarNum"));
        Integer videoCollectionNum= Integer.valueOf(request.getParameter("videoCollectionNum"));
        String videoUrl= request.getParameter("videoUrl");
        String videoImg= request.getParameter("videoImg");
        Integer visit= Integer.valueOf(request.getParameter("visit"));
        Video video = new Video(null,videoId,videoUser,videoType,videoDescribe,videoExamine,videoIsviewable,videoUploadDate,videoStarNum,videoCollectionNum,videoUrl,videoImg,visit);
        int result = videoService.insert(video);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(result);
    }

    /**
     * 展示视频信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void select(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Video> videos = videoService.queryAll();
        response.setContentType("application/json;charset=utf-8");
        Gson gson = new Gson();
        //List 对象转换成 JSON 字符串
        String videosStr = gson.toJson(videos);
        response.getWriter().write(videosStr);

    }

    /**
     * 删除视频
     * @param request
     * @param response
     * @throws IOException
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Long id = Long.valueOf(request.getParameter("id"));
        int result = videoService.delete(id);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(result);

    }
    /**
     * 修改视频信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void upData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String videoId = request.getParameter("name");
        Integer videoUser= Integer.valueOf(request.getParameter("videoUser"));
        Integer videoType = Integer.valueOf(request.getParameter("videoType"));
        String videoDescribe = request.getParameter("videoDescribe");
        Integer videoExamine = Integer.valueOf(request.getParameter("videoExamine"));
        Integer videoIsviewable = Integer.valueOf(request.getParameter("videoIsviewable"));
        String videoUploadDate= request.getParameter("videoUploadDate");
        Integer videoStarNum = Integer.valueOf(request.getParameter("videoStarNum"));
        Integer videoCollectionNum= Integer.valueOf(request.getParameter("videoCollectionNum"));
        String videoUrl= request.getParameter("videoUrl");
        String videoImg= request.getParameter("videoImg");
        Integer visit= Integer.valueOf(request.getParameter("visit"));
        Video video = new Video(id, videoId, videoUser, videoType, videoDescribe, videoExamine, videoIsviewable, videoUploadDate, videoStarNum, videoCollectionNum, videoUrl,videoImg,visit);
        int i = videoService.upDate(video);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(i);
    }

    /**
     * 前台获取视频数据
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void page(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), 5);
        Page<Video> page = videoService.page(pageNo, pageSize);

        //基于 Ajax
//        response.setContentType("text/html;charset=utf-8");
//        Gson gson = new Gson();
//        String s=gson.toJson(page);
//        response.getWriter().write(s);

        //基于 JSP
        request.setAttribute("page", page);
        request.getRequestDispatcher("/admin/product_list.jsp").forward(request, response);
    }

    /**
     * 后台管理获取数据
     * @param request
     * @param response
     * @throws IOException
     */
    public void adminPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), 5);
        Page<Video> page = videoService.page(pageNo, pageSize);
        //基于 Ajax
        response.setContentType("text/html;charset=utf-8");
        Gson gson = new Gson();
        String s=gson.toJson(page);
        response.getWriter().write(s);
    }

    /**
     * 获取该用户的视频数量
     * @param request
     * @param response
     * @throws IOException
     */
    public void videoCount(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        int totalCount = videoService.queryForUserVideoTotalCount(userId);
        response.getWriter().print(totalCount);
    }

}
